<!-- 技术咨询解答记录 -->
<script setup lang="ts">
import {
  getSolutionPage,
  getSolutionForm,
  deleteSolution,
} from "@/api/tecConsult/index";
import {
  ConsultationQuery,
  ConsultationPageVO,
  ConsultationForm,
  tutorSolution,
} from "@/api/tecConsult/types";
import { CloseBold } from "@element-plus/icons-vue";

const queryFormRef = ref(ElForm); // 查询表单
const conFormRef = ref(ElForm); //技术咨询表单

const viewDialog = ref(false); //判断是否为查看
const loading = ref(false); //加载状态
const queryParams = reactive<ConsultationQuery>({
  page: 1,
  pageSize: 10,
});
const startTime = ref("");
const endTime = ref("");
const total = ref(0); // 数据总数
const pageData = ref<ConsultationPageVO[]>(); // 用户分页数据

watch(startTime, (newVal) => {
  if (newVal) {
    queryParams.startTime = newVal;
  }
});
watch(endTime, (newVal) => {
  if (newVal) {
    queryParams.endTime = newVal;
  }
});

// 弹窗对象
const dialog = reactive({
  visible: false,
  width: 800,
  title: "",
});

// 技术咨询表单数据
const formData = reactive<ConsultationForm[]>([]);

/** 查询 */
function handleQuery() {
  loading.value = true;
  getSolutionPage(queryParams)
    .then(({ data }) => {
      pageData.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  startTime.value = "";
  endTime.value = "";
  queryParams.page = 1;
  queryParams.startTime = undefined;
  queryParams.endTime = undefined;
  handleQuery();
}

//打开弹窗
function openDialog(id?: string) {
  dialog.visible = true;
  if (id) {
    getSolutionForm(id).then(({ data }) => {
      formData.splice(0, formData.length, ...data);
    });
  }
  viewDialog.value = true;
  dialog.title = "用户信息";
}

/**
 * 关闭弹窗
 */
function closeDialog() {
  dialog.visible = false;
  viewDialog.value = false;
  conFormRef.value.resetFields();
  conFormRef.value.clearValidate();
}

/** 删除解答 */
function handleDelete(id?: string, itemIndex?: number, proIndex?: number) {
  console.log(id);
  console.log(itemIndex);
  console.log(proIndex);

  if (id) {
    ElMessageBox.confirm("确认删除解答记录?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      deleteSolution(id).then(() => {
        // if (index !== undefined) {
        //   formData.tutorSolution?.splice(index, 1);
        // }\
        formData[itemIndex].tutorSolution.splice(proIndex, 1);
        ElMessage.success("删除成功");
        resetQuery();
      });
    });
  }
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <!-- 技术咨询列表 -->
    <div class="search-container">
      <!-- ref="queryFormRef" :model="queryParams" -->
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        :inline="true"
        label-width="100px"
        class="form-container"
      >
        <el-form-item label="咨询人" prop="create_by">
          <el-input
            placeholder="咨询人"
            clearable
            style="width: 200px"
            v-model="queryParams.create_by"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="咨询时间" prop="create_date">
          <el-date-picker
            v-model="startTime"
            class="!w-[200px]"
            type="datetime"
            placeholder="开始时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
          <span class="time">~</span>
          <el-date-picker
            v-model="endTime"
            class="!w-[200px]"
            type="datetime"
            placeholder="截止时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item> -->
        <el-form-item label="解答导师" prop="createBy">
          <el-input
            placeholder="解答导师"
            clearable
            style="width: 200px"
            v-model="queryParams.createBy"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="解答时间" prop="createDate">
          <el-date-picker
            v-model="startTime"
            class="!w-[200px]"
            type="datetime"
            placeholder="开始时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
          <span class="time">~</span>
          <el-date-picker
            v-model="endTime"
            class="!w-[200px]"
            type="datetime"
            placeholder="截止时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item> -->

        <el-form-item class="handleQuery">
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table v-loading="loading" :data="pageData" style="width: 100%" border>
        <el-table-column
          label="咨询人"
          align="center"
          prop="create_by"
          width="120"
        />
        <el-table-column
          label="技术需求"
          align="center"
          prop="demand"
          width="450"
        />
        <el-table-column
          label="咨询时间"
          align="center"
          width="180"
          prop="create_date"
        />
        <el-table-column
          label="解答时间"
          align="center"
          prop="createDate"
          width="180"
        >
          <template #default="{ row }">
            <div v-for="(item, index) in row.tutorSolution" :key="index">
              {{ item.createDate }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="解答导师"
          align="center"
          prop="createBy"
          width="120"
        >
          <template #default="{ row }">
            <div v-for="(item, index) in row.tutorSolution" :key="index">
              {{ item.createBy }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" width="150" align="center">
          <!-- #default="scope" -->
          <template #default="scope">
            <!-- v-hasPerm="['sys:user:reset_pwd']"自定义命令，查看用户是否有使用该功能的权限 -->
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row.id)"
              ><i-ep-view />查看</el-button
            >

            <!-- v-hasPerm="['sys:user:delete']" -->
            <!-- <el-button
              type="primary"
              link
              size="small"
              @click="handleDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      :width="dialog.width"
      append-to-body
      @close="closeDialog"
    >
      <el-form :model="formData" label-width="100px" ref="conFormRef">
        <el-form-item
          label="技术需求"
          prop="demand"
          v-if="formData.length > 0 && formData[0]"
        >
          <el-input
            type="textarea"
            placeholder="请输入技术问题"
            v-model="formData[0].demand"
            :disabled="viewDialog"
        /></el-form-item>
        <el-form-item
          label="项目简介"
          prop="project_brief"
          v-if="formData.length > 0 && formData[0]"
        >
          <el-input
            :rows="3"
            type="textarea"
            placeholder="请输入项目简介"
            v-model="formData[0].project_brief"
            :disabled="viewDialog"
          />
        </el-form-item>

        <el-form-item
          label="技术问题"
          prop="question"
          v-if="formData.length > 0 && formData[0]"
        >
          <el-input
            :rows="3"
            type="textarea"
            placeholder="请输入技术问题"
            v-model="formData[0].question"
            :disabled="viewDialog"
          />
        </el-form-item>
        <el-form-item
          label="期望解决方案"
          prop="expectation"
          v-if="formData.length > 0 && formData[0]"
        >
          <el-input
            :rows="3"
            type="textarea"
            placeholder="请输入期望解决方案"
            v-model="formData[0].expectation"
            :disabled="viewDialog"
          />
        </el-form-item>
        <el-form-item
          label="咨询时间"
          prop="create_date"
          v-if="formData.length > 0 && formData[0]"
        >
          <el-date-picker
            placeholder="请选择咨询时间"
            v-model="formData[0].create_date"
            :disabled="viewDialog"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item
          label="咨询人"
          prop="create_by"
          v-if="formData.length > 0 && formData[0]"
        >
          <el-input
            placeholder="请输入咨询人"
            v-model="formData[0].create_by"
            :disabled="viewDialog"
        /></el-form-item>
        <h3 style="margin-top: 20px; font-weight: normal">导师解答:</h3>
        <div v-for="(item, index) in formData" :key="'item-' + index">
          <div
            v-for="(project, proIndex) in item.tutorSolution as any"
            :key="'progect' + proIndex"
            class="tutorSolution"
          >
            <div style="flex-grow: 1">
              <el-form-item label="解答内容" prop="solution">
                <el-input
                  :rows="3"
                  type="textarea"
                  placeholder="请输入解答内容"
                  v-model="project.solution"
                  :disabled="viewDialog"
                />
              </el-form-item>
              <el-form-item label="解答时间" prop="createDate">
                <el-date-picker
                  placeholder="解答时间"
                  style="width: 100%"
                  v-model="project.createDate"
                  :disabled="viewDialog"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                />
              </el-form-item>
              <el-form-item label="解答导师:" prop="createBy">
                <el-input
                  placeholder="请输入解答导师"
                  v-model="project.createBy"
                  :disabled="viewDialog"
                />
              </el-form-item>
            </div>
            <el-button
              text
              circle
              style="margin-left: 10px"
              @click="handleDelete(project.id, index, proIndex)"
              ><el-icon><CloseBold /></el-icon
            ></el-button>
          </div>
        </div>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="closeDialog">确 认</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

.form-container {
  display: flex;
  flex-wrap: wrap;
}

.handleQuery {
  margin-left: auto;
}

.time {
  margin: 0 10px;
}

.tutorSolution {
  position: relative;
  display: flex;
  align-items: start;
  justify-content: space-between;
  padding: 10px;
  margin-bottom: 20px;
  border: 1px solid #ccc;
}
</style>
